import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
@Component({
    selector: 'app-role-authority',
    templateUrl: './role-authority.component.html',
})
export class RoleAuthorityComponent implements OnInit {
    validateForm;
    activeData: any = {};
    tableLoading = false;
    menuTree = [];
    menuList = [];
    isIndeterminate = false;
    isAllDisplayDataChecked = false;
    ngOnInit() {
        this.message.setBreadcrumb({
            breadcrumbs: [
            {name: '系统角色管理'}
        ]});
        this.validateForm = this.fb.group({
            name: [null, [ Validators.required]],
            memo: [null],
            status: [null],
        });
        this.activeData = JSON.parse(sessionStorage.getItem('roleData'));
        const {name, memo, status} = this.activeData;
        const data = {name, memo, status};
        this.validateForm.setValue(data);
        this.getMenuList();
    }
    changeExpand(e, item) {
        if (!e) {
            item.children.forEach(element => {
                element.expand = e;
            });
        }
    }
    getMenuList() {
        this.tableLoading = true;
        this.apiService.post('菜单列表', {}).then((res: any) => {
            this.menuList = res.data.list;
            this.menuTree = this.getTree(res.data.list);
            this.tableLoading = false;
        }).catch(res => {
            this.tableLoading = false;
        });
    }
    getTree(list) {
        const array = [], obj = {};
        list.forEach(element => {
            if (element.parentId === 0) {
                array.push(element);
            }
            element.expand = true;
            obj[element.id] = element;
        });
        list.forEach(element => {
            if (obj[element.parentId]) {
                element.$parent = obj[element.parentId];
                obj[element.parentId].children ? obj[element.parentId].children.push(element) : obj[element.parentId].children = [element];
            }
        });
        return array;
    }
    checkAll(e) {
        this.menuList.forEach(item => (item.checked = e));
    }
    submit() {
        const array = [];
        this.menuList.forEach(item => {
            if (item.checked) {array.push(item.id); }
        });
        this.activeData.pageIds = array;
        this.apiService.post('编辑角色', this.activeData).then((res: any) => {
            this.nzMessageService.success('编辑成功');
            this.router.navigate(['systemMgmt/roleMgmt']);
        })
    }
    refreshStatus(data) {
        if (data.checked) {
            if (data.children) {
                data.children.map(element => {
                    element.checked = true;
                    if(element.children){
                        element.children.map( e => e.checked = true);
                    }
                });
            }
            if (data.$parent) {
                data.$parent.checked = true;
                if (data.$parent.$parent) {
                    data.$parent.$parent.checked = true;
                }
            }
        } else {
            if (data.children) {
                data.children.map(element => {
                    element.checked = false;
                    if(element.children){
                        element.children.map( e => e.checked = false);
                    }
                });
            }
            if (data.$parent) {
                const a = data.$parent.children.filter(e => e.checked === true);
                console.log(a);
                if (a.length < 1) { data.$parent.checked = false; }
                if (data.$parent.$parent) {
                    data.$parent.$parent.checked = false;
                    const a = data.$parent.$parent.children.filter(e => e.checked === true);
                    if (a.length < 1) { data.$parent.$parent.checked = false; }
                }
            }
        }
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService
    ) { }
}
